package com.itlaobing.aop.schema;

import org.junit.Test;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Arrays;
import java.util.List;

public class Introduction {

    public @Test void test(){

        String configLocaltion = "com/itlaobing/aop/schema/Introduction.xml";
        AbstractApplicationContext context = new ClassPathXmlApplicationContext(configLocaltion);

        Snake s = context.getBean("snake",Snake.class);
        System.out.println(s.getName());
        System.out.println(s.toString());

        if (s instanceof List){
            List list = (List) s;
            list.add("烤肉串");
            System.out.println(Arrays.toString(list.toArray()));
        }

        System.out.println(s.toString());
        context.close();
    }

    public @Test void analyzeProxyClass() {
        String configLocaltion = "com/itlaobing/aop/schema/Introduction.xml";
        AbstractApplicationContext context = new ClassPathXmlApplicationContext(configLocaltion);

        Snake s = context.getBean("snake",Snake.class);

        Class<?> c = s.getClass();
        System.out.println("Proxy Class: "+ c.getName());

        Class<?> pc = c.getSuperclass();
        System.out.println("Parent Class: "+ pc.getName());

        Class<?>[] inters = c.getInterfaces();
        for(Class<?> inter : inters){
            System.out.println(inter);
        }

        System.out.println( "- - - - - - - - - - - - - - - - - - - -" );

        // 取c类中直接声明的字段
        Field[] fields = c.getDeclaredFields();
        for (int i = 0; i < fields.length; i++) {
            Field field = fields[i];
            String modifier = Modifier.toString(field.getModifiers());
            Class<?> type = field.getType();
            String name = field.getName();
            System.out.println(  modifier + " " + type.getSimpleName() + " " + name );
        }

        StringBuffer buffer = new StringBuffer();
        // 取c类中直接声明的方法
        Method[] methods = c.getDeclaredMethods();
        for (int i = 0; i < methods.length; i++) {
            Method method = methods[i];
            String modifier = Modifier.toString(method.getModifiers());
            Class<?> type = method.getReturnType();

            String name = method.getName();
            Class<?>[] paraTypes = method.getParameterTypes();
            buffer.setLength(0);
            buffer.append("(");

            if (paraTypes!=null){
                for (int j = 0; j < paraTypes.length; j++) {
                    Class<?> pt = paraTypes[j];
                    buffer.append(pt.getSimpleName());
                    buffer.append(',');
                }
            }

            int index = buffer.lastIndexOf(","); //lastIndexOf 查询最后（？）的位置，并返回
            if (index != -1){
                buffer.deleteCharAt(index);
            }

            buffer.append(")");
            System.out.println(  modifier + " " + type.getSimpleName() + " " + name + buffer.toString() );
        }
        context.close();
    }
}
